layui.config({
    base : "js/"
}).extend({
    "address" : "address"
});
layui.use(['form','layer','address'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        address = layui.address;

    if ($("#id").val()!='' && $("#id").val()!=null  && $("#id").val()!=undefined )   {
        var province = $("#province_value").val();
        var city = $("#city_value").val();
        var area = $("#area_value").val();
        address.provinces(province,city,area);
    }else{
        address.provinces1();
    }
    layer.config({
        extend: 'myskin/style.css', //加载您的扩展样式
        skin: 'my_skin'
    });
    $(document).on('click','#admin_name',function(){
        var admin_id=$("#admin_id").val();
        var index=layer.open({
            title:'选择管理员',
            type:2,
            area: ['560px', '460px'],
            skin: "my_skin",
            resize: false,
            content: ["/toSelectAdmin?admin_id="+admin_id,'no'],
            btn: ['确定','关闭'],
            btnAlign: 'c',
            // shadeClose: true,
            yes: function(index,layerObj){
                //当点击‘确定'按钮的时候，获取弹出层返回的值
              var iframeWindow = layerObj.find('iframe')[0].contentWindow;
                var checkStatus = iframeWindow.layui.table.checkStatus('adminListTable');
                var data = checkStatus.data;
                var admin_id="";
                var admin_name="";
                if(data.length >0){
                    for(var i=0;i<data.length;i++){
                        if(i<data.length-1){
                            admin_id += data[i].id + ",";
                            admin_name += data[i].name + ",";
                        }else{
                            admin_id += data[i].id;
                            admin_name += data[i].name;
                        }
                    }
                    $("#admin_id").val(admin_id);
                    $("#admin_name").val(admin_name);
                }else{
                    /*layer.msg("请先选择管理员！");*/
                    $("#admin_id").val(admin_id);
                    $("#admin_name").val(admin_name);
                }
                //最后关闭弹出层
                layer.close(index);
            },
            cancel: function(){
                layer.close(index);
                //右上角关闭回调
            }
        });
    });

    //添加验证规则
    form.verify({
        name : function(value, item){
            var data;
            var id =$("#id").val();
            $.ajax({
                async: false,
                type: "POST",
                url: "/checkSchoolNameUnique",
                data: "name="+value+"&id="+id,
                success: function(msg){
                    data=msg;
                }
            });
            if (data == "true") {
                return "学校已经存在！";
            }
        }
    });
    form.on("submit(addSchool)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.post("/schoolAdd", {
            id: $("#id").val(),//id
            name: $(".name").val(),
            short_name: $(".short_name").val(),
            province: $("#province").val(),
            province_name: $("#province").find("option:selected").text(),
            city: $("#city").val(),
            city_name: $("#city").find("option:selected").text(),
            area: $("#area").val(),
            area_name: $("#area").find("option:selected").text(),
            bz:$("#bz").val(),
            admin_id: $("#admin_id").val(),
            admin_name: $("#admin_name").val()
        }, function (data) {
            if (data =="success") {
                top.layer.close(index);
                top.layer.msg("操作成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            }else if (data == "error") {
                top.layer.msg("操作失败！");
            }
        });
/*        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("用户添加成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);*/
        return false;
    });

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());

});